﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Web.Utility;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.Business;
using BySoft.Server.Consts;
using BySoft.Server.Common.Enum;
using OneSoft.EntityCore;
using System.Data;
using BySoft.Web.Controls;

namespace BySoft.Web.Site.SysManage
{
    public partial class ParamerterList :  AuthBasePage
    {
        #region 事件
        /// <summary>
        ///   页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UIHelper.InsertFirstItem(ddlIsValid);
                ddlIsValid.SelectedIndex = 0;
                BindParameterList(0);
            }
        }


        /// <summary>
        ///  参数查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnParaSearch_Click(object sender, EventArgs e)
        {
            BindParameterList(0);
        }

        #region  sgridView事件


        /// <summary>
        ///   sgvParaList行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void sgvParaList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int id = CommonMethod.ConvertToInt(e.CommandArgument);
                SysParameterEntity entity = BySoft.Server.Business.SelectionCatche.GetParamerterByID(id);
                SysParametertypeEntity type = BySoft.Server.Business.SelectionCatche.GetParameterTypeByID(entity.ParameterTypeID);
                if (type != null && type.IsSolidify == ((int)BaseEnum.IsSolidify.Yes).ToString())
                {
                    UIHelper.AlertMessage(this.Page, Message.SY_DeleteSolidParameter);
                    return;
                }
                if (SysParameter.Delete(entity))
                {
                    UIHelper.AlertMessage(this.Page, Message.DeleteSuccess);
                    SysParameter.RefreshWebCaching();//刷新缓存
                    BindParameterList(0);
                    base.LogAction(BaseEnum.ActionType.Delete, entity.ParameterID.Value.ToString(), true, null, new EntityBase[] { entity });
                }
                else
                {
                    UIHelper.AlertMessage(this.Page, Message.DeleteFail);
                    base.LogAction(BaseEnum.ActionType.Delete, entity.ParameterID.Value.ToString(), false, null, new EntityBase[] { entity });
                }
            }
        }

        /// <summary>
        /// sgvParaList 行绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void sgvParaList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                #region 删除绑定
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }
                #endregion

                //是否有效
                if (e.Row.Cells[5].Text == ((int)BaseEnum.CommonSatus.Started).ToString())
                {
                    e.Row.Cells[5].Text = "是";
                }
                else
                {
                    e.Row.Cells[5].Text = "否";
                }

                if (e.Row.Cells[4].Text.Length > 30)
                {
                    e.Row.Cells[4].ToolTip = e.Row.Cells[4].Text;
                    e.Row.Cells[4].Text = CommonMethod.GetUnicodeSubString(e.Row.Cells[4].Text, 30, "...");//描述太长，截取显示

                }
            }
        }

        #endregion

        #region 分页


        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            BindParameterList(e.PageIndex);
        }
        #endregion

        #endregion

        #region Method

        /// <summary>
        ///  参数查询
        /// </summary>
        public void BindParameterList(int pageIndex)
        {
            DataTable dt = BySoft.Server.Business.SelectionCatche.QueryParameter(txtTypeName_para.Text.Trim(),
                txtParaCode.Text.Trim(), txtParameterName.Text.Trim(), ddlIsValid.SelectedValue);
            pcData.RecordCount = dt.Rows.Count;
            pcData.CurrentIndex = pageIndex;
            grdResult.DataSource = CommonMethod.GetPagedTable(dt, pageIndex, pcData.PageSize);
            if (dt.Rows.Count != 0)
            {
                grdResult.DataBind();
            }
            else
            {
                UIHelper.BindNullSGridView(grdResult);
            }
            UIHelper.RunScript(this.UpdatePanel1, "iframeAutoFit();");
        }
        #endregion     
    }
}
